package com.fingard.dsp.bank.directbank.cmb03;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.text.StringHelper;

/**
 * 招行北京分行代付系统批量代付
 * 
 * @author Cruise
 * @date 2018/6/26
 */
public class CMB03BatPayRec extends CMB03Base implements IBatPayRec {
	@Override
	public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988)
			throws Exception {
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
		retAts.reqSeqID = p_req1988.reqBatchNO;

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(
					p_req1988.ownAct, "");
			if (actItem != null) {
				JSONObject tmpJsonRequest = new JSONObject();

				JSONObject tmpReqHeader = new JSONObject();
				tmpReqHeader.put("CODE", CMB03TransCode.singlePay_1988_198801);
				tmpReqHeader.put("BKDT", "");
				tmpReqHeader.put("BKSER", "");
				tmpReqHeader.put("CMDT",
						Format.DateTime14Format.format(new Date()));
				tmpReqHeader.put("CMSER", p_req1988.reqBatchNO);
				tmpReqHeader.put("PRDNO", actItem.merchantId);
				tmpReqHeader.put("PARTER", actItem.inMerchantId);
				tmpReqHeader.put("PDTNO", actItem.goodNo);

				tmpJsonRequest.put("HEAD", tmpReqHeader);

				JSONObject tmpJsonBody = new JSONObject();
				tmpJsonBody.put("BATCHNO", p_req1988.reqBatchNO);
				tmpJsonBody.put("BATCHTYP", "2");
				tmpJsonBody.put("TOTNUM", p_req1988.totalNum);
				tmpJsonBody.put("TOTAMT", p_req1988.totalAmt);
				tmpJsonBody.put("REMARK", p_req1988.useType);
				tmpJsonBody.put("DEACCTNO", p_req1988.ownAct);

				JSONArray detailJsonArray = new JSONArray();
				while (p_req1988.readRecord()) {
					JSONObject tmpDetailParams = new JSONObject();
					tmpDetailParams.put("YURREF",
							p_req1988.readedDetail.askNumber);
					tmpDetailParams.put("CRACCTNO",
							p_req1988.readedDetail.recAct);
					tmpDetailParams.put("CRACCTNAME",
							p_req1988.readedDetail.recName);
					tmpDetailParams.put("CRBANKNAME",
							p_req1988.readedDetail.recBankLocation);
					tmpDetailParams.put("CRCHGNO",
							p_req1988.readedDetail.cnapsCode);
					tmpDetailParams.put("CRBANKADR",
							p_req1988.readedDetail.recBankLocation);
					tmpDetailParams.put("TRXTXT",
							p_req1988.readedDetail.useType + "+"
									+ p_req1988.readedDetail.askNumber);
					tmpDetailParams.put("TRNAMT",
							p_req1988.readedDetail.payAmount);
					if ("1".equals(p_req1988.readedDetail.isPrivate)) {
						tmpDetailParams.put("CRACCTCLASS", "2");
					} else {
						tmpDetailParams.put("CRACCTCLASS", "1");
					}
					detailJsonArray.add(tmpDetailParams);
				}
				tmpJsonBody.put("BATCPAYX", detailJsonArray);
				tmpJsonRequest.put("BODY", tmpJsonBody);

				JSONObject tmpRootJsonReq = new JSONObject();
				tmpRootJsonReq.put("ROOT", tmpJsonRequest);

				String tmpStrRet = sendToBank(tmpRootJsonReq, actItem);

				retAts.respCode = "0";
				if (StringHelper.hasAnyChar(tmpStrRet)) {
					tmpStrRet = tmpStrRet.substring(0,
							tmpStrRet.indexOf("&signTime"));
					JSONObject respBizContent = JSON.parseObject(tmpStrRet);

					JSONObject respBodyJson = respBizContent.getJSONObject(
							"ROOT").getJSONObject("BODY");
					String tmpRespCode = respBodyJson.getString("TRNRESP");
					if ("CMBMB99".equals(tmpRespCode)) {
						retAts.respInfo = respBodyJson.getString("RTNMSG");
						retAts.transState = respBodyJson.getString("RTNCOD");
						retAts.payInfoCode = respBodyJson.getString("RTNCOD");
					} else {
						retAts.respInfo = respBodyJson.getString("TRNMSG");
						retAts.transState = tmpRespCode;
						retAts.payInfoCode = tmpRespCode;
					}
				} else {
					retAts.respInfo = "银行响应内容为空,请联系银行咨询!";
					retAts.transState = PaymentState.FAIL_Batch;
					retAts.payInfoCode = FailCode.E9981;
				}
			} else {
				retAts.respCode = "0";
				retAts.respInfo = "未找到账号:[" + p_req1988.ownAct
						+ "]商户配置信息,请检查config.xml中'actSet'节点相关配置信息!";
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = FailCode.E9981;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeRetToATS();
		return retAts;
	}

	@Override
	public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998)
			throws Exception {
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
		retAts.reqSeqID = p_req1998.reqBatchNO;

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(
					p_req1998.ownAct, "");
			if (actItem != null) {
				JSONObject tmpJsonRequest = new JSONObject();

				JSONObject tmpReqHeader = new JSONObject();
				tmpReqHeader.put("CODE", CMB03TransCode.singlePay_1998_198801);
				tmpReqHeader.put("BKDT", "");
				tmpReqHeader.put("BKSER", "");
				tmpReqHeader.put("CMDT",
						Format.DateTime14Format.format(new Date()));
				tmpReqHeader.put("CMSER", p_req1998.reqBatchNO);
				tmpReqHeader.put("PRDNO", actItem.merchantId);
				tmpReqHeader.put("PARTER", actItem.inMerchantId);
				tmpReqHeader.put("PDTNO", actItem.goodNo);

				tmpJsonRequest.put("HEAD", tmpReqHeader);

				JSONObject tmpDetailParams = new JSONObject();
				tmpDetailParams.put("BATCQRYX", p_req1998.reqBatchNO);
				tmpJsonRequest.put("BODY", tmpDetailParams);

				JSONObject tmpRootJsonReq = new JSONObject();
				tmpRootJsonReq.put("ROOT", tmpJsonRequest);

				String tmpStrRet = sendToBank(tmpRootJsonReq, actItem);

				retAts.respCode = "0";
				if (StringHelper.hasAnyChar(tmpStrRet)) {
					tmpStrRet = tmpStrRet.substring(0,
							tmpStrRet.indexOf("&signTime"));
					JSONObject respBizContent = JSON.parseObject(tmpStrRet);

					JSONObject respBodyJson = respBizContent.getJSONObject(
							"ROOT").getJSONObject("BODY");
					String tmpRespCode = respBodyJson.getString("TRNRESP");
					if ("CMBMB99".equals(tmpRespCode)) {
						String tmpRetCode = respBodyJson.getString("RTNCOD");
						if ("0000".equals(tmpRetCode)) {
							String batchStatus = respBodyJson.getString("BATSTS");
							if ("FIN".equals(batchStatus)) {
								JSONArray jsonDetail = respBodyJson.getJSONArray("BATCQRYZ");
								if (jsonDetail.size() > 0) {
									for (int i = 0; i < jsonDetail.size(); i++) {
										JSONObject detailJson = jsonDetail
												.getJSONObject(i);

										RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
										tmpRetDetail.recAct = detailJson
												.getString("CRACCTNO");
										tmpRetDetail.recName = detailJson
												.getString("CRACCTNAME");
										tmpRetDetail.postScript = detailJson
												.getString("YURREF");
										tmpRetDetail.payAmount = detailJson
												.getString("TRNAMT");
										tmpRetDetail.memo = detailJson
												.getString("RESULT");
										tmpRetDetail.result = detailJson
												.getString("STATUS");
										tmpRetDetail.payInfoCode = detailJson
												.getString("RESULT");
										retAts.writeRetDetail(tmpRetDetail);
									}
								}
							} else {
								retAts.respInfo = respBodyJson.getString("RTNMSG");
								retAts.transState = batchStatus;
								retAts.payInfoCode = batchStatus;
							}
						} else {
							if ("1005".equals(tmpRetCode)) {
								if (canBeNotExist(p_req1998, retAts, 45)) {
									retAts.respCode = "0";
									retAts.respInfo = respBodyJson.getString("RTNMSG");
									retAts.transState = tmpRespCode + "+" + respBodyJson.getString("RTNMSG");
									retAts.payInfoCode = tmpRespCode;
									retAts.writeAfterDetail();
									return retAts;
								}
							} else {
								retAts.respCode = "-1";
								retAts.respInfo = respBodyJson.getString("RTNMSG");
							}
						}
					} else {
						retAts.respCode = "-1";
						retAts.respInfo = respBodyJson.getString("TRNMSG");
					}
				} else {
					retAts.respInfo = "银行响应内容为空,请联系银行咨询!";
					retAts.transState = PaymentState.FAIL_Batch;
					retAts.payInfoCode = FailCode.E9981;
				}
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账号:[" + p_req1998.ownAct
						+ "]商户配置信息,请检查config.xml中'actSet'节点相关配置信息!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeAfterDetail();
		return retAts;
	}

	@Override
	public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188)
			throws Exception {
		return null;
	}

	@Override
	public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198)
			throws Exception {
		return null;
	}
}